/****************************************************************************
* COSC1283/1284 - Programming Techniques
* Semester 1 2011 Assignment #1
* Full Name        : Le Yan  
* Student Number   : 3262302 
* Yallara Username : s3262302 
* Course Code      : BP232 
* Program Code     : COSC1284  
* Start up code provided by Christopher Hoobin and Xiaodong Li
****************************************************************************/

#include "assign1.h"

//print menu
void printmenu( char *menu[], int n)
{
  int i;
  for(i=0; i<n; i++)
    printf("%s", menu[i]);
}



/* Read next integer
   if fail to read then return the invalid character,
   otherwise return 0;
   the integer which read from stdin will be stored in num
*/
int NextInt(int *num);
int isinSet(char c);


//options
void option1(int *optionStats);
void option2(int *optionStats);
void option3(int *optionStats);
void option4(int *optionStats);
void option5(int *optionStats);
void option6(int *optionStats);

int main(void)
{
   int optionStats[NUM_OPTION_STATS];

   char *mainmenu[N] = { "Main Menu:\n",
                    "1) ASCII/Hexadecimal/Binary table generator\n",
                    "2) Simple clock display\n",
                    "3) Current date/time display\n",
                    "4) Converting a Roman numeral\n",
                    "5) Minesweeper\n",
                    "6) Session summary\n",
                    "7) Exit\n\n",
                    "Select your option: " 
                   };
   char input, tmp; 
   int i;

   //init optionStats
   for(i=0; i<NUM_OPTION_STATS; i++)
     optionStats[i] = 0; 

   //print main menu;
   while(1)
   {
     printmenu(mainmenu, N);
     input = getc(stdin);

            
     if( input == '\n')
      continue;

     if( (tmp=getc(stdin)) != '\n' )
     {
        readRestOfLine();
        printf("You input is invalid!\n\n");
        continue;
     }
     else
     {
        ungetc(tmp, stdin);
     }

     readRestOfLine();
     switch (input)
     {
       case '1':
                 option1(optionStats);
                 break;
       case '2':
                 option2(optionStats);
                 break;
       case '3': 
                 option3(optionStats);
                 break;
       case '4':
                 option4(optionStats); 
                 break;
       case '5':
                 option5(optionStats);
                 break;
       case '6': 
                 option6(optionStats);
                 break;
       //Exit 
       case '7':  
                 return EXIT_SUCCESS;
       default:
                 printf("Your input is invalid!\n\n");
                 continue;
     };
   } 
   //return EXIT_SUCCESS;
}

int isinSet(char c)
{ 
   char *set = "MDCLXVI";
   int i;
   for(i=0; i<strlen(set); i++)
   {
      if(c == set[i])
       return 1; 
   } 

   return 0;
}



void option6(int *optionStats)
{
  sessionSummary(optionStats);
}


void option5(int *optionStats)
{
  int m, n, mines;
  char c;
  while(1)
  { 
    while(1)  
    { 
     printf("Mine sweeper\n");
     printf("------------\n"); 
     printf("THe number of lines (m): ");
     
     c = getc(stdin);
    
     //if user input 'enter' then return to main menu
     if ( c == '\n' )
       return;
     else
       ungetc(c, stdin);
   
     //read m 
     if ( NextInt(&m) )
     {
       printf("Please input valid m !\n");
       readRestOfLine();
       continue;
     } 
     else if( m>100 || m<=0 )
     {
       printf("m should be more than 0 and less of equal to 100!\n");
       readRestOfLine();
       continue;
     }
     readRestOfLine();
     break;
    }

    while(1)
    {
     printf("The number of columns (n): "); 
    
     c = getc(stdin);
    
     //if user input 'enter' then return to main menu
     if ( c == '\n' )
       return;
     else
       ungetc(c, stdin);

     //read n 
     if ( NextInt(&n) )
     {
       printf("Please input valid n !\n");
       readRestOfLine();
       continue;
     } 
     else if( n>100 || n<=0 )
     {
       printf("n should be more than 0 and less of equal to 100!\n");
       readRestOfLine();
       continue;
     } 
     readRestOfLine();
     break;
    }

  
    while(1)
    { 
     printf("The number of mines (numMines): ");

     //if user input 'enter' then return to main menu
     c = getc(stdin);
     if ( c == '\n' )
       return;
     else
       ungetc(c, stdin);

     //read number of mines 
     if ( NextInt(&mines) )
     {
       printf("Please input valid number of mines !\n");
       readRestOfLine();
       continue;
     } 
     else if( mines <=0 || mines > m*n-1 )
     {
       printf("Number should be more than 0 and less of equal to %d!\n", m*n-1);
       readRestOfLine();
       continue;
     } 
     readRestOfLine();
     break; 
    }  

    //  printf("%d %d %d\n", m, n, mines); 
    minesweeper(optionStats, m, n, mines);
    printf("\nTry again? (y/n): ");
  
    c = getc(stdin);
    printf("%c\n", c);
    if( c != 'y' && c != 'Y' )
    {
      readRestOfLine();
      break;
    }
    readRestOfLine();
     
 } 
}


void option4(int *optionStats)
{ 
   char st[MAXROMAN], c;
   int i, len; 
    
    
   while(1)
   {
    
     printf("Converting a Roman numeral\n");
     printf("--------------------------\n\n");
     printf("Enter a Roman numeral (with a maximal length of 10): ");

     while( (c=getc(stdin)) == ' ' || c == '	'); 
   
     if( c == '\n' )
       return;
  
     for(i=0, len=0; len<MAXROMAN && c != '\n'; i++, len++)
     {
       if( !isinSet(c) )
       {         
          break;
       }

       st[i] = c;
       c = getc(stdin);
     }     
     
     if( len >= MAXROMAN)
     {
       printf("\nYour input is too long, maximal length is 10\n");
       readRestOfLine();
       continue; 
     } 

     if( c != '\n' )
     {
          printf("\nPlease input valid Roman numerals!\n\n");
          readRestOfLine();
          continue;
     } 
     st[i] = '\0';

     convertRomanNumeral(optionStats, st);
     break;
   }
}


void option3(int *optionStats)
{
  displayClock2(optionStats);
  
}

void option2(int *optionStats)
{
    int h , m;
    char c;

    while(1)
    {
       printf("Simple clock display\n");
       printf("--------------------\n\n");
       printf("Enter <hours> <minutes>: ");

       c = getc(stdin);
       if( c == '\n' )
         return;
       else
         ungetc(c, stdin);

       if ( NextInt(&h) )
       {
           printf("Please input valid hours and minutes!\n");
           readRestOfLine();
           continue;
       }
        
       if ( NextInt(&m) )
       { 
           printf("Please input valid hours and minutes!\n");
           readRestOfLine();
           continue;
       } 
       //printf("%d %d\n", h, m);

       if( displayClock(optionStats, h, m) ) 
       {
         printf("Please input valid hours and minutes!\n");
         readRestOfLine();
         continue; 
       }       
       readRestOfLine();
       break;
    } 
}


//option1
void option1(int *optionStats)
{
  char st[5];
  int i, n, j;
  char c;

  while(1)
  {
     
    printf("\n\nASCII to binary and hexadecimal table generator\n");
    printf("-----------------------------------------------\n\n");
    printf("Enter a string (1-5 characters): ");
    
    i = 0;
    while( (c=fgetc(stdin)) != '\n' && i < 6)
    {
      st[i++] = c; 
    }

    //check the length of input
    if( c != '\n' )
    {
      printf("\nYour input is too long!\n");
      readRestOfLine();
      continue;
    }

    //if user just input enter then return to menu    
    if( i == 0 )
      break;

    st[i] = '\0';
    asciiTranslation(optionStats, st);
    return;     
  } 

}

/*Read Next int*/
int NextInt(int *num)
{
  char c;
  int sign = 1; 
  while( (c=getc(stdin)) == ' ' || c == '	' );
  
  if( c == '-' )
  {
    sign = -1;
    c = getc(stdin);
  }

  if( !isdigit(c) )
  {
    ungetc(c, stdin);   
    return c;
  }

  *num = 0;
  while( isdigit(c) )
  {
    *num = (*num)*10 + c - '0'; 
    c = getc(stdin);
  }
 
  ungetc(c, stdin);
  *num *= sign;
  return 0;
} 



